package org.example.secondkill.service.impl;

import org.example.secondkill.dao.UserDao;
import org.example.secondkill.domain.User;
import org.example.secondkill.domain.vo.LoginVO;
import org.example.secondkill.exception.BaseException;
import org.example.secondkill.response.ResultCode;
import org.example.secondkill.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Autowired
    private UserDao userDao;

    @Override
    public boolean register(String nickname, String password) {
        return userDao.register(nickname, password) > 0;
    }

    @Override
    public void login(LoginVO loginVO) {
        String nickname = loginVO.getNickname();
        String password = loginVO.getPassword();
        User user = userDao.selectByNickname(nickname);

        // 用户名不存在
        if (user == null) {
            throw new BaseException(ResultCode.USER_NOT_EXIST);
        }

        // 用户名-密码校验
        if (!user.getPassword().equals(password)) {
            // 密码错误
            throw new BaseException(ResultCode.PASSWORD_ERROR);
        }
    }

    @Override
    public void creatManyUsers(int number) {
        for (int i = 0; i < number; i++) {
            userDao.register("holy", "shit");
        }
    }
}
